package ar.edu.unicen.exa.isistan.archassis.view.cases;

import java.util.Collection;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;

import ar.edu.unicen.exa.isistan.archassis.ia.api.db.ListCases;
import ar.edu.unicen.exa.isistan.archassis.model.listeners.ICasesListener;
import ar.edu.unicen.exa.isistan.archassis.view.ListComposite;
import ar.edu.unicen.exa.isistan.archassis.view.cases.caseitem.CaseComposite;
import ar.edu.unicen.exa.isistan.archassis.view.dialogs.SelectCaseDialog;

public class CasesComposite extends ListComposite implements ICasesListener {

	/**
	 * Create the composite.
	 * @param parent
	 * @param selectCaseDialog 
	 * @param style
	 */
	public CasesComposite(Composite parent, SelectCaseDialog selectCaseDialog) {
		super(parent, SWT.NONE);
		this.selectCaseDialog = selectCaseDialog; 
	}


	public void setCases(Collection<ListCases> casesList) {
		disposeChildrens();
		
		if (casesList == null) {
			return;
		}
		
		child.setLayout(new FormLayout());
		
		Composite lastItem = null;
		
		for (ListCases aiResult : casesList) {
			selected = aiResult;
			CaseComposite child2 = new CaseComposite(child, SWT.NONE);
			child2.setCase(aiResult);
			child2.setCaseListener(this);
			
			FormData fd_lblNewLabel = new FormData();
		    fd_lblNewLabel.right = new FormAttachment(100, -5);
		    
		    fd_lblNewLabel.left = new FormAttachment(0, 5);
			if (lastItem == null) {
				fd_lblNewLabel.top = new FormAttachment(0, 10);
			} else {
				fd_lblNewLabel.top = new FormAttachment(lastItem, 5);
			}
			lastItem = child2;
			child2.setLayoutData(fd_lblNewLabel);

		}
		endChange();
	
	}
	
	private SelectCaseDialog selectCaseDialog;
	private ListCases selected;
	public ListCases getSelected() {
		return selected;
	}

	@Override
	public void change(ListCases casesList) {
		selected = casesList;
		selectCaseDialog.exitCase();
	}
	
}
